//
// Created by 李传智 on 2018/4/12.
//

#include "VideoThread.h"
#include "Logger.h"
#include <thread>

using namespace std;

void sleep(int ms) {
    chrono::milliseconds duration(ms);
    this_thread::sleep_for(duration);
}

// 启动线程
bool VideoThread::start() {
    isExist = false;
    thread t(&VideoThread::innerMain, this);
    // 当前线程放弃对新创建线程的控制
    t.detach();
    return true;
}

void VideoThread::setPause(bool isPause) {
    this->isPause = isPause;
    //等待100ms
    for (int i = 0; i < 10; i++) {
        if (isPause == this->isPausing) {
            break;
        }
        sleep(10);
    }
}

bool VideoThread::isPaused() {
    isPausing = isPause;
    return isPause;
}

void VideoThread::innerMain() {
    isRunning = true;
    //LOG_I("线程函数进入");
    main();
    //LOG_I("线程函数退出");
    isRunning = false;
}

bool VideoThread::stop() {
    isExist = true;
    for (int i = 0; i < 200; ++i) {
        if (!isRunning) {
            //LOG_I("thread stop success");
            return true;
        }
        sleep(1);
    }
    //LOG_E("thread stop timeout");
    return true;
}